package com.yt.server.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * <p>Title: DateFormats</p>
 * <p>Copyright: </p>
 * <p>@Author <a href="mailto:sunwindrain@sohu.com">Andrew(zhuyi)</a></p>
 * <p>Date 2007-4-12</p>
 */
public class DateFormats {
	
	public static String DATE_PATTERN="yyyy-MM-dd";
	public static String FULL_TIME_PATTERN="yyyy-MM-dd HH:mm:ss.SSS";
	public static String NORMAL_TIME_PATTERN="yyyy-MM-dd HH:mm:ss";
	
	
	public static Date stringToDate(String date) {
		if(date==null || date.equals(""))
			return null;
		String pattern = "yyyy-MM-dd hh:mm:ss";
		if (date != null && date.length() <= 10) pattern = "yyyy-MM-dd";
		return stringToDate(date, pattern);
	}
	
	public static String formatTime(long sec) {
		StringBuffer sb = new StringBuffer();
		if (sec > 0) {
			long hour = sec / (60 * 60);
			long minute = (sec % (60 * 60)) / 60;
			long second = sec % 60;
			if (hour > 0) sb.append(hour).append("小时");
			if (hour > 0 || minute > 0) sb.append(minute).append("分");
			if (second > 0 && sec < 60) sb.append(second).append("秒");
		}
		else {
			sb.append("0秒");
		}
		return sb.toString();
	}
	
	/**
	 * @param DateString
	 * @param pattern 
	 * @return
	 */
	public static Date stringToDate(String DateString, String pattern) {
		Date result = null;
		String patternFormat = DATE_PATTERN;
		if (!pattern.equals(""))
			patternFormat = pattern;
		DateFormat f = new SimpleDateFormat(patternFormat);
		// ParsePosition pos = new ParsePosition(0);
		try {
			result = f.parse(DateString);
		} catch (Exception e) {
			result = null;
		}
		return result;
	}

	/**
	 * @param DateString
	 * @param pattern 
	 * @param defaultDate
	 * @return
	 */
	public static Date stringToDate(String DateString, String pattern,Date defaultDate) {
		Date result = defaultDate;
		String patternFormat = FULL_TIME_PATTERN;
		if (!pattern.equals(""))
			patternFormat = pattern;
		DateFormat f = new SimpleDateFormat(patternFormat);
		// ParsePosition pos = new ParsePosition(0);
		try {
			result = f.parse(DateString);
		} catch (Exception e) {
			System.out.println("error: convert Date!");
		}
		return result;
	}
	
	/**
	 * @param DateString
	 * @param pattern 
	 * @param defaultDate
	 * @return
	 */
	public static Date stringToDate(String DateString, String pattern,int initvalue) {
		Date result = new Date(initvalue);
		String patternFormat = DATE_PATTERN;
		if (!pattern.equals(""))
			patternFormat = pattern;
		DateFormat f = new SimpleDateFormat(patternFormat);
		// ParsePosition pos = new ParsePosition(0);
		try {
			result = f.parse(DateString);
		} catch (Exception e) {
			System.out.println("error: convert Date!");
		}
		return result;
	}
	
	public static String dateToString(long date, String pattern) {
		return dateToString(date > 0 ? new Date(date):null, pattern);
	}
		
	public static String dateToString(long date) {
		return dateToString(date > 0 ? new Date(date):null);
	}
	
	/**
	 * @param date
	 * @param pattern format example: yyyy-MM-dd
	 * @return
	 */
	public static String dateToString(Date date, String pattern) {
		String result = "";
		if (date != null) {
			String patternFormat = DATE_PATTERN;
			if (!pattern.equals(""))
				patternFormat = pattern;
			try {
				DateFormat f = new SimpleDateFormat(patternFormat);
				result = f.format(date);
			} catch (Exception e) {
				System.out.println("error: convert Date!");
			}
		}
		return result;
	}

	/**
	 * @param date
	 * @param pattern format example: yyyy-MM-dd
	 * @return
	 */
	public static String dateToString(Date date) {
		return dateToString(date,DATE_PATTERN);
	}

	public final static java.util.Date stringToTime(String dateString) {
		java.util.Date dateTime = null;
		String patternFormat = DATE_PATTERN;
		DateFormat dateFormat = new SimpleDateFormat(patternFormat);// 设定格式	
		try {
			dateFormat.setLenient(false);
			dateTime = dateFormat.parse(dateString);// util类型
		} 
		catch (Exception e) {

		}

		return dateTime;
	}

	public final static java.util.Date stringToTime(String dateString, String pattern) {
		java.util.Date dateTime = null;
		String patternFormat = DATE_PATTERN;
		if (pattern != null && !pattern.equals("")) patternFormat = pattern;
		DateFormat dateFormat = new SimpleDateFormat(patternFormat);// 设定格式
		try {
			dateFormat.setLenient(false);
			dateTime = dateFormat.parse(dateString);// util类型
		} 
		catch (Exception e) {

		}
		return dateTime;
	}
	
	public static String timeToString(long time, String pattern) {
		if(time==0L)
			return "-";
		return timeToString(new Date(time), pattern);
	}

	/**
	 * @param time
	 * @param pattern 格式串 如：yyyy-MM-dd
	 * @return
	 */
	public static String timeToString(java.util.Date time, String pattern) {
		if(time == null) return "";
		String result = "";
		String patternFormat = DATE_PATTERN;
		if (!pattern.equals(""))
			patternFormat = pattern;
		DateFormat f = new SimpleDateFormat(patternFormat);
		try {
			result = f.format(time);
		} 
		catch (Exception e) {
			System.out.println("error: convert time!");
		}
		return result;
	}
	

	/**
	 * @param date
	 * @param days
	 * @return 时间的天＋days之后的时间
	 */
	public static Date dateDayAdd(Date date, int days) {
		Calendar  cal = new GregorianCalendar();
		cal.setTime(date);
		cal.add(Calendar.DATE,days);
		return cal.getTime();
	}
 
	/**
	 * @param date
	 * @return 时间的天＋1之后的时间
	 */
	public static Date dateAddOne(Date date) {
		return dateDayAdd(date,1);
	}
}

